package com.gxuwz.attendance.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.gxuwz.attendance.entity.User;
import com.gxuwz.attendance.service.UserService;
import com.gxuwz.attendance.util.JsonUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends BaseAction implements ModelDriven<User>,Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private Logger logger = LoggerFactory.getLogger(LoginAction.class);
	private static String LOGIN_INDEX = "/page/system/login.jsp";
	private static String LOGIN = "/page/system/index.jsp";
	@Autowired
	private UserService service;
	private Map<String, String> map;
	private String vCode;
	
	/**
	 * 登录
	 */
	public void login() {
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, String>();
		
		String flag = service.findByUser(user);
		
		logger.info(flag);
		
		map.put("type", "error");
		if (vCode.isEmpty() || "".equals(vCode)) {
			map.put("msg", "验证码不能为空");
			JsonUtil.toJons(ServletActionContext.getResponse(), map);
			return ;
		}
		
		String vCodeGenerator = (String) request.getSession().getAttribute("vCode");
		if(StringUtils.isEmpty(vCodeGenerator)) {
			map.put("msg", "长时间未操作，会话已失效，请刷新后重试!");
			JsonUtil.toJons(ServletActionContext.getResponse(), map);
			return ;
		}
		
		if(!vCodeGenerator.toUpperCase().equals(vCode.toUpperCase())) {
			map.put("msg", "验证码错误");
			JsonUtil.toJons(ServletActionContext.getResponse(), map);
			return ;
		}
		
		
		if(flag.equals("empty")) {
//			map.put("type", "error");
			map.put("msg", "不存在该用户!");
		}else if(flag.equals("no")) {
//			map.put("type", "error");
			map.put("msg", "密码错误!");
		}else if(flag.equals("yes")) {
			map.put("type", "success");
			map.put("msg", "登陆成功");
		}
		
		forwardView = LOGIN;
		JsonUtil.toJons(ServletActionContext.getResponse(), map);
		
	}
	/**
	 * 退出登录
	 * @return
	 */
	public String out() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(0);
		forwardView = LOGIN_INDEX;
		return SUCCESS;
	}
	
	/**
	 * 跳转到登录页面
	 * @return
	 */
	public String forward() {	
		forwardView = LOGIN_INDEX;
		
		return SUCCESS;
	}
		
		

	@Override
	public void prepare() throws Exception {
		if(user == null) {
			user = new User();
		}
		
	}

	@Override
	public User getModel() {
		return user;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String getvCode() {
		return vCode;
	}
	public void setvCode(String vCode) {
		this.vCode = vCode;
	}
	
}
